package org.esgi.xml;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.*;
import org.xml.sax.InputSource;

public class XpathRequest {

	public void test(String[] args) {
		
		XPathFactory fac = XPathFactory.newInstance();
		XPath xp = fac.newXPath();
		
		String req;
		XPathExpression exp = null;
		req = "count(//*)";
		try {
			exp = xp.compile(req);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Double d = null;
		try {
			d = (Double)exp.evaluate(new InputSource(args[0]), XPathConstants.NUMBER);
		} catch (XPathExpressionException e1) {
			e1.printStackTrace();
		}
		System.out.println("Nombre d'elements : "+d);
		
		req = "/*";
		try {
			exp = xp.compile(req);
		} catch (Exception e) {
			e.printStackTrace();
		}
		NodeList res = null;
		try {
			res = (NodeList)exp.evaluate(new InputSource(args[0]), XPathConstants.NODESET);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("Nom element racine :"+ res.item(0).getNodeName());
	}
}
